import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Jiange
 * @create 2021-05-12-16:42
 */

class ShareResource{
    private int number = 1;
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();


    public void printf5(){
        lock.lock();
        try{
            while (number != 1){
                condition1.await();
            }
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            number = 2;
            condition2.signal();
        }catch (Exception e){

        }finally {
            lock.unlock();
        }
    }

    public void printf10(){
        lock.lock();
        try{
            while (number != 2){
                condition2.await();
            }
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            number = 3;
            condition3.signal();
        }catch (Exception e){

        }finally {
            lock.unlock();
        }
    }

    public void printf15(){
        lock.lock();
        try{
            while (number != 3){
                condition3.await();
            }
            for (int i = 0; i < 15; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            number = 1;
            condition1.signal();
        }catch (Exception e){

        }finally {
            lock.unlock();
        }
    }
}

public class ThreadOrder {
    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();
        new Thread(()->{
            for (int i = 0; i < 3; i++) {
                shareResource.printf5();
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 3; i++) {
                shareResource.printf10();
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 3; i++) {
                shareResource.printf15();
            }
        },"D").start();
    }
}
